package views;
/**
 * EDCAnalyzeViewer 
 * Viewer that shows DTDs via EDCTableAnalyzeModel
 */
import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
//import javax.swing.table.TableRowSorter;
import parser.Parser;

import ui.EDCTableAnalyzeModel;

public class EDCAnalyzeViewer extends EDCDefaultViewer {

	private static final long serialVersionUID = 1L;
	private EDCTableAnalyzeModel tModel;
	private JTable table;
//    private TableRowSorter<EDCTableAnalyzeModel> sorter;
    private Parser p;
    
    protected String[] columnToolTips = {"bla1","bla2","bla3","bla4"};
	
    public EDCAnalyzeViewer(){
    	tModel = new EDCTableAnalyzeModel();
		setTitle("Analyze viewer");
    	
		//init parser
		initParser();
		
		tModel.setData(p.dtdNames,p.stats);

//        sorter = new TableRowSorter<EDCTableAnalyzeModel>(tModel);
		table = new JTable(tModel);
//		table.setRowSorter(sorter);
//        table.setFillsViewportHeight(true);
		table.setPreferredScrollableViewportSize(new Dimension(650,600));
		
		//Columns size
		setColumnWidth(table,0,250);
		setColumnWidth(table,1,80);
		setColumnWidth(table,2,50);
		setColumnWidth(table,3,50);
		setColumnWidth(table,4,80);
		setColumnWidth(table,5,80);
		
		//Create the scroll pane and add the table to it.
		JScrollPane jsp = new JScrollPane(table);
		
		//Add the scroll pane to this JInternalFrame.
		getContentPane().add(jsp);
		showHorScroll(table, true);
    }
    
    /**This method shows the horizontal scroll bar when required.
     * It's being called in the constructors.
     */
	public void showHorScroll(JTable t, boolean show) {
		if (show){
			t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}else{
			t.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		}
	}
	
	/**this method should be called to set the column
	   *at pColumn index to a width of pWidth
	   */
	public void setColumnWidth(JTable t, int pColumn, int pWidth){
		//get the column model
		TableColumnModel colModel = t.getColumnModel();
		//get the column at index pColumn, and set its preferred width
		colModel.getColumn(pColumn).setPreferredWidth(pWidth);
	}
	
	public JTable getTable(){
		return table;
	}
	
	public EDCTableAnalyzeModel getEDCTableModel(){
		return tModel;
	}
	
	private void initParser(){
		p=new Parser();
		p.parseDTDs();
	}
    
}
